package com.example.library;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class UserDao {
    private DatabaseHelper dbHelper;

    public UserDao(Context context) {
        dbHelper = new DatabaseHelper(context);
    }

    // 注册用户
    public boolean register(String name, String studentId, String password, String contact, String userType) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Cursor cursor = db.query("user", null, "studentId=?", new String[]{studentId}, null, null, null);
        if (cursor.moveToFirst()) {
            cursor.close();
            db.close();
            return false; // 已存在
        }
        cursor.close();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("studentId", studentId);
        values.put("password", password);
        values.put("contact", contact);
        values.put("userType", userType);
        values.put("creditScore", 100);
        values.put("status", 1);
        long result = db.insert("user", null, values);
        db.close();
        return result != -1;
    }

    // 登录校验
    public String login(String studentId, String password, String userType) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("user", null, "studentId=? AND password=? AND userType=? AND status=1",
                new String[]{studentId, password, userType}, null, null, null);
        String name = null;
        if (cursor.moveToFirst()) {
            name = cursor.getString(cursor.getColumnIndexOrThrow("name"));
        }
        cursor.close();
        db.close();
        return name; // 返回用户名，null表示失败
    }

    // 获取所有用户信息
    public List<User> getAllUsers() {
        List<User> userList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("user", null, null, null, null, null, null);
        while (cursor.moveToNext()) {
            User user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndexOrThrow("id")));
            user.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
            user.setStudentId(cursor.getString(cursor.getColumnIndexOrThrow("studentId")));
            user.setPassword(cursor.getString(cursor.getColumnIndexOrThrow("password")));
            user.setContact(cursor.getString(cursor.getColumnIndexOrThrow("contact")));
            user.setUserType(cursor.getString(cursor.getColumnIndexOrThrow("userType")));
            user.setCreditScore(cursor.getInt(cursor.getColumnIndexOrThrow("creditScore")));
            user.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
            int isBlacklistedIndex = cursor.getColumnIndex("isBlacklisted");
            if (isBlacklistedIndex != -1) {
                user.setBlacklisted(cursor.getInt(isBlacklistedIndex) == 1);
            }
            userList.add(user);
        }
        cursor.close();
        db.close();
        return userList;
    }

    // 获取黑名单人数
    public int getBlacklistCount() {
        int count = 0;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM user WHERE status=2", null);
        if (cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }
        cursor.close();
        db.close();
        return count;
    }

    // 获取用户总数
    public int getUserCount() {
        int count = 0;
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT COUNT(*) FROM user", null);
        if (cursor.moveToFirst()) {
            count = cursor.getInt(0);
        }
        cursor.close();
        db.close();
        return count;
    }

    // 修改密码
    public boolean changePassword(String studentId, String userType, String newPassword) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("password", newPassword);
        int rows = db.update("user", values, "studentId=? AND userType=?", new String[]{studentId, userType});
        db.close();
        return rows > 0;
    }

    // 通过学号和用户类型查找用户
    public User getUserByStudentIdAndType(String studentId, String userType) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.query("user", null, "studentId=? AND userType=?", new String[]{studentId, userType}, null, null, null);
        User user = null;
        if (cursor.moveToFirst()) {
            user = new User();
            user.setId(cursor.getInt(cursor.getColumnIndexOrThrow("id")));
            user.setName(cursor.getString(cursor.getColumnIndexOrThrow("name")));
            user.setStudentId(cursor.getString(cursor.getColumnIndexOrThrow("studentId")));
            user.setPassword(cursor.getString(cursor.getColumnIndexOrThrow("password")));
            user.setContact(cursor.getString(cursor.getColumnIndexOrThrow("contact")));
            user.setUserType(cursor.getString(cursor.getColumnIndexOrThrow("userType")));
            user.setCreditScore(cursor.getInt(cursor.getColumnIndexOrThrow("creditScore")));
            user.setStatus(cursor.getInt(cursor.getColumnIndexOrThrow("status")));
        }
        cursor.close();
        db.close();
        return user;
    }
} 